package org.come.control;

import org.come.action.FromServerAction;
import org.come.bean.ShaoXiangLimit;
import org.come.bean.ShaoXiangLimitResultBean;
import org.come.entity.Goodstable;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import java.util.concurrent.ConcurrentHashMap;

public class ShaoXiangControl implements FromServerAction {
    public void controlMessFromServer(String mes, String type) {

        if (mes.startsWith("GETLIMIT")) {
            String[] result = mes.split("\\|");
            if (result[1] != null) {
                ShaoXiangLimitResultBean sa = GsonUtil.getGsonUtil().getgson().fromJson(result[1], ShaoXiangLimitResultBean.class);
                UserMessUntil.setAllshaoxianglimit(sa.getAllshaoxianglimit());
                UserMessUntil.setAllshaoxiang(sa.getAllshaoxiang());
            }
        }else if(mes.startsWith("UPDATELIMIT")){
            String[] result = mes.split("\\|");
            if (result[1] != null) {
                ShaoXiangLimit shlimit = GsonUtil.getGsonUtil().getgson().fromJson(result[1], ShaoXiangLimit.class);
                ConcurrentHashMap<String, ShaoXiangLimit> allshlimit = UserMessUntil.getAllshaoxianglimit();
                ShaoXiangLimit oshlimit = allshlimit.get(shlimit.getName());
                if(oshlimit==null){
                    UserMessUntil.getAllshaoxianglimit().put(shlimit.getName(),shlimit);
                }else{
                    oshlimit.setNum(shlimit.getNum());
                }
            }
        }else if(mes.startsWith("MODGOODS")){
            String[] result = mes.split("\\@");
            if (result[1] != null) {
                Goodstable goodstable = GsonUtil.getGsonUtil().getgson().fromJson(result[1], Goodstable.class);
                if(GoodsListFromServerUntil.getChoseGoodsList()!=null && GoodsListFromServerUntil.getChoseGoodsList().length>0){
                    for(int i=0; i<GoodsListFromServerUntil.getChoseGoodsList().length;i++){
                        if(GoodsListFromServerUntil.getChoseGoodsList()[i]==null || GoodsListFromServerUntil.getChoseGoodsList()[i].getRgid() == null){
                             continue;
                        }
                        if(goodstable.getRgid().compareTo(GoodsListFromServerUntil.getChoseGoodsList()[i].getRgid()) == 0){
                            GoodsListFromServerUntil.getChoseGoodsList()[i] = goodstable;
                        }
                    }
                }
            }

        }
    }
}
